package net.oschina.dajiangnan.tree.demo;

import net.oschina.dajiangnan.tree.ExceptionNode;
import net.oschina.dajiangnan.tree.Node;
import net.oschina.dajiangnan.tree.TreeConfig;
import net.oschina.dajiangnan.tree.TreeExecute;
import org.apache.commons.io.IOUtils;

import java.io.InputStream;
import java.util.Scanner;

public class Main {
    private static final String NODE_PACKAGE_PREFIX = "net.oschina.dajiangnan.tree.demo.";
    private static final String DOT_PATH = "demo.dot";

    public static void main(String[] args) throws Exception {
        //使用Dot脚本构造流程的tree结构,返回头结点
        Node rootNode = new TreeConfig().getRootNode(() -> {
            InputStream inputStream = Main.class.getClassLoader().getResourceAsStream(DOT_PATH);
            return IOUtils.toString(inputStream, "UTF-8");
        }, NODE_PACKAGE_PREFIX);


        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            //控制台输入,观察流程执行路径
            X x = new X();
            x.setId(scanner.next());
            //执行流程,返回叶子节点
            Node node = TreeExecute.me().execute(x, rootNode);
            //上下文中保存执行路径及各节点执行时间
            System.out.println(x.traceInfo());
            if(node instanceof LeafRandomC){
                //结果为叶子结点
            }
            if(node instanceof ExceptionNode){
                //抛出任何异常流程终止,流转到ExceptionNode节点
            }
        }
    }
}
